package com.ciaojian.core.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.enums.business.CouponCodeOriginEnum;
import com.ciaojian.core.mapper.CouponMapper;
import com.ciaojian.core.model.Coupon;
import com.ciaojian.core.model.CouponCode;
import com.ciaojian.core.model.CouponPackage;
import com.ciaojian.core.model.Member;
import com.ciaojian.core.util.LogUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Atlakyn
 */
@Service
public abstract class CouponService<M extends BaseMapper<T>, T> extends ServiceImpl<CouponMapper, Coupon> implements IService<Coupon> {
    /**
     * 获取券码的过期时间
     *
     * @param couponId 券码id
     * @return 券码过期时间
     */
    public Date getCouponExpiredTime(Integer couponId) {
        return baseMapper.getCouponExpiredTimeById(couponId);
    }

    public Boolean addGetNum(Integer id) {
        return this.baseMapper.addGetNumById(id);
    }

    public void getCouponCode(Integer memberId, Integer userId) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("coupon").eq("id", memberId);
        Member dMember = new Member().selectOne(queryWrapper);
        if (dMember == null || StrUtil.isBlank(dMember.getCoupon())) {
            return;
        }
        // 券码类型字符串
        String couponStr = dMember.getCoupon();
        if (StrUtil.isNotEmpty(couponStr)) {
            // 按 , 切割字符串
            String[] coupons = couponStr.split(",");
            // 券类型不为空表示充值的会员可以赠送卡券
            QueryWrapper<CouponCode> queryCouponCodeWrapper = new QueryWrapper<>();
            queryCouponCodeWrapper.isNull("user_id");
            queryCouponCodeWrapper.eq("status", 0);
            queryCouponCodeWrapper.isNotNull("outside_code");
            queryCouponCodeWrapper.ne("outside_code", "");
            queryCouponCodeWrapper.last("limit 1");
            for (String coupon : coupons) {
                queryCouponCodeWrapper.eq("coupon_id", coupon);
                // 更新 券码
                CouponCode dCouponCode = new CouponCode().selectOne(queryCouponCodeWrapper);
                // 如果查不到 券码,表示数量不够,进入下一次循环
                if (dCouponCode == null) {
                    LogUtil.writePayLog("券码%s不存在", coupon);
                    continue;
                }
                dCouponCode.setUserId(userId);
                dCouponCode.setStatus(1);
                dCouponCode.setOrigin(CouponCodeOriginEnum.buy_member);
                dCouponCode.updateById();

                // 更新 券码类型 已领取数量
                this.addGetNum(Integer.valueOf(coupon));
            }
        }
    }

    /**
     * 券领取数量+1
     *
     * @param couponPackageId 券包
     */
    public void addGetNumByCouponPackageId(Integer couponPackageId) {
        CouponPackage couponPackage = new CouponPackage().selectById(couponPackageId);
        String[] split = couponPackage.getCoupons().split(",");
        baseMapper.addGetNumByIds(split);
    }

    /**
     * 检查券库存是否充足
     *
     * @param couponId 券ID
     * @return TRUE 足够
     */
    public Boolean checkStockSufficient(Integer couponId) {
        return baseMapper.operateStock(couponId);
    }
}
